package fr.upstlse.glre.client.espaceperso.profil;

import net.mygwt.ui.client.Style;
import net.mygwt.ui.client.event.BaseEvent;
import net.mygwt.ui.client.event.SelectionListener;
import net.mygwt.ui.client.fx.Draggable;
import net.mygwt.ui.client.widget.Button;
import net.mygwt.ui.client.widget.ContentPanel;
import net.mygwt.ui.client.widget.Info;
import net.mygwt.ui.client.widget.ToolButton;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;

import fr.upstlse.glre.client.RfgConstante;
import fr.upstlse.glre.client.controler.ControlerService;
import fr.upstlse.glre.client.controler.ControlerServiceAsync;
import fr.upstlse.glre.client.vo.EtudiantVOGWT;
import fr.upstlse.glre.client.widgets.FailureMessageBox;
import fr.upstlse.glre.client.widgets.InfoMessageBox;
import fr.upstlse.glre.client.widgets.TextField;

/**
 * Construit le panneau de profil d'un étudiant. Ce panneau affiche
 * textuellement le profil d'un étudiant. Il offre la possibilité de le modifier
 * par l'intermédiaire d'un formulaire
 * 
 * @author michael
 * 
 */
public class EtudiantProfil extends Composite {

	/**
	 * Panneau principal. Dispose verticalement les éléments.
	 */
	VerticalPanel panel;

	/**
	 * Panneau offrant un certain nombre de widgets. Il contient le panneau de
	 * contenu.
	 */
	ContentPanel profilPanel;

	/**
	 * Panneau à disposition vertical Contient soit le panneau d'affichage du
	 * projet soit le panneau d'édition du profil
	 */
	VerticalPanel profilContent;

	/**
	 * Panneau HTML contenant le profil au format texte.
	 */
	HTML profilReadingPanel;

	/**
	 * Panneau FORM contenant le profil au format formulaire pour l'édition.
	 */
	FormPanel profilEditingPanel;

	EtudiantVOGWT etudiant;

	/**
	 * Variable d'état pour le panneau de contenu. Vrai si le panneau courant
	 * est l'édition du profil, faux sinon
	 */
	boolean editionMode = false;

	ControlerServiceAsync controlerService;

	/**
	 * Construit le panneau de profil. L'initialise avec le contenu en mode
	 * lecture (affichage du profil) Initialise le service.
	 * 
	 * @param pEtudiant
	 *            Etudiant dont on souhaite obtenir le profil
	 */
	public EtudiantProfil(EtudiantVOGWT pEtudiant) {
		etudiant = pEtudiant;
		
		controlerService = (ControlerServiceAsync) GWT
				.create(ControlerService.class);
		ServiceDefTarget lEndPoint = (ServiceDefTarget) controlerService;
		String lServiceUrl = GWT.getModuleBaseURL() + "controlerService";
		lEndPoint.setServiceEntryPoint(lServiceUrl);

		panel = new VerticalPanel();
		createProfilPanel();
		readingMode();
		editionMode = false;
		initWidget(panel);
	}

	private void createProfilPanel() {
		profilPanel = new ContentPanel(Style.HEADER | Style.COLLAPSE
				| Style.RESIZE, "my-cpanel-small");
		profilPanel.setId(RfgConstante.PROFIL_PANEL_ID);
		profilPanel.setBorders(true);

		// Draggable panel
		Draggable lDraggable = new Draggable(profilPanel, profilPanel
				.getHeader());

		// Tool Button
		ToolButton lSwithToEditionMode = new ToolButton(".my-tool-gear");
		lSwithToEditionMode.setStyleAttribute("background",
				" transparent url(images/edit.png) no-repeat");
		lSwithToEditionMode
				.setToolTip(RfgConstante.PROFIL_PANEL_BUTTON_SWITCH_TOOLTIP);
		lSwithToEditionMode.addSelectionListener(new SelectionListener() {
			public void widgetSelected(BaseEvent be) {
				if (!editionMode) {
					profilContent.remove(profilReadingPanel);
					editionMode();
					editionMode = true;
				} else {
					profilContent.remove(profilEditingPanel);
					profilContent.add(profilReadingPanel);
					editionMode = false;
				}
			}

		});
		profilPanel.getHeader().setText(RfgConstante.PROFIL_PANEL_HEADER_TEXT);
		profilPanel.getHeader().addWidget(lSwithToEditionMode);

		profilContent = new VerticalPanel();
		profilPanel.add(profilContent);
		panel.add(profilPanel);
	}

	private void editionMode() {
		// formulaire sans action
		profilEditingPanel = new FormPanel();
		profilEditingPanel.setAction(".");
		profilEditingPanel.setMethod(FormPanel.METHOD_POST);

		VerticalPanel lFormPanel = new VerticalPanel();
		profilEditingPanel.add(lFormPanel);

		ContentPanel lLoginPasswordPanel = new ContentPanel(Style.HEADER,
				"blockEdition");
		lLoginPasswordPanel.getHeader().setText(
				RfgConstante.PROFIL_EDITING_BLOCK_IDENTIFIANT_HEADER_TEXT);
		final TextField lLogin = new TextField(
				RfgConstante.PROFIL_EDITING_FIELD_LOGIN_LABEL,
				RfgConstante.PROFIL_EDITING_FIELD_LOGIN_NAME, etudiant
						.getLogin());
		lLoginPasswordPanel.add(lLogin);

		final TextField lMotdepasse = new TextField(
				RfgConstante.PROFIL_EDITING_FIELD_MOTDEPASSE_LABEL,
				RfgConstante.PROFIL_EDITING_FIELD_MOTDEPASSE_NAME, etudiant
						.getPassword());
		lLoginPasswordPanel.add(lMotdepasse);
		lFormPanel.add(lLoginPasswordPanel);

		ContentPanel lInformationPanel = new ContentPanel(Style.HEADER,
				"blockEdition");
		lInformationPanel.getHeader().setText(
				RfgConstante.PROFIL_EDITING_BLOCK_INFO_HEADER_TEXT);
		final TextField lNom = new TextField(
				RfgConstante.PROFIL_EDITING_FIELD_NOM_LABEL,
				RfgConstante.PROFIL_EDITING_FIELD_NOM_NAME, etudiant.getNom());
		lInformationPanel.add(lNom);

		final TextField lPrenom = new TextField(
				RfgConstante.PROFIL_EDITING_FIELD_PRENOM_LABEL,
				RfgConstante.PROFIL_EDITING_FIELD_PRENOM_NAME, etudiant
						.getPrenom());
		lInformationPanel.add(lPrenom);

		final TextField lEmail = new TextField(
				RfgConstante.PROFIL_EDITING_FIELD_EMAIL_LABEL,
				RfgConstante.PROFIL_EDITING_FIELD_EMAIL_NAME, etudiant
						.getEmail());
		lInformationPanel.add(lEmail);

		ContentPanel lTelephonePanel = new ContentPanel(Style.HEADER,
				"blockEdition");
		lTelephonePanel.getHeader().setText(
				RfgConstante.PROFIL_EDITING_BLOCK_TELEPHONE_HEADER_TEXT);
		final TextField lTelephoneFixe = new TextField(
				RfgConstante.PROFIL_EDITING_FIELD_TELEPHONEFIXE_LABEL,
				RfgConstante.PROFIL_EDITING_FIELD_TELEPHONEFIXE_NAME, etudiant
						.getTelephonefixe());
		lTelephonePanel.add(lTelephoneFixe);

		final TextField lTelephonePortable = new TextField(
				RfgConstante.PROFIL_EDITING_FIELD_TELEPHONEPORTABLE_LABEL,
				RfgConstante.PROFIL_EDITING_FIELD_TELEPHONEPORTABLE_NAME,
				etudiant.getTelephoneportable());
		lTelephonePanel.add(lTelephonePortable);
		lInformationPanel.add(lTelephonePanel);

		ContentPanel lAdressePanel = new ContentPanel(Style.HEADER,
				"blockEdition");
		lAdressePanel.getHeader().setText(
				RfgConstante.PROFIL_EDITING_BLOCK_ADRESSE_HEADER_TEXT);
		final TextField lAdresseNumero = new TextField(
				RfgConstante.PROFIL_EDITING_FIELD_ADRESSE_NUMERO_LABEL,
				RfgConstante.PROFIL_EDITING_FIELD_ADRESSE_NUMERO_NAME,
				(new Integer(etudiant.getAdressenumero())).toString());
		lAdressePanel.add(lAdresseNumero);

		final TextField lAdresseVoie = new TextField(
				RfgConstante.PROFIL_EDITING_FIELD_ADRESSE_VOIE_LABEL,
				RfgConstante.PROFIL_EDITING_FIELD_ADRESSE_VOIE_NAME, etudiant
						.getAdressevoie());
		lAdressePanel.add(lAdresseVoie);

		final TextField lAdresseCodePostal = new TextField(
				RfgConstante.PROFIL_EDITING_FIELD_ADRESSE_CODEPOSTAL_LABEL,
				RfgConstante.PROFIL_EDITING_FIELD_ADRESSE_CODEPOSTAL_NAME,
				etudiant.getAdressecodepostal());
		lAdressePanel.add(lAdresseCodePostal);

		final TextField lAdresseVille = new TextField(
				RfgConstante.PROFIL_EDITING_FIELD_ADRESSE_VILLE_LABEL,
				RfgConstante.PROFIL_EDITING_FIELD_ADRESSE_VILLE_NAME, etudiant
						.getAdresseville());
		lAdressePanel.add(lAdresseVille);

		final TextField lAdressePays = new TextField(
				RfgConstante.PROFIL_EDITING_FIELD_ADRESSE_PAYS_LABEL,
				RfgConstante.PROFIL_EDITING_FIELD_ADRESSE_PAYS_NAME, etudiant
						.getAdressepays());
		lAdressePanel.add(lAdressePays);
		lInformationPanel.add(lAdressePanel);

		lFormPanel.add(lInformationPanel);

		Button lSubmit = new Button(
				RfgConstante.PROFIL_EDITING_FIELD_SUBMIT_LABEL);
		lSubmit.addSelectionListener(new SelectionListener() {
			public void widgetSelected(BaseEvent be) {
				etudiant.setLogin(lLogin.getValue());
				etudiant.setPassword(lMotdepasse.getValue());
				etudiant.setNom(lNom.getValue());
				etudiant.setPrenom(lPrenom.getValue());
				etudiant.setEmail(lEmail.getValue());
				etudiant.setTelephonefixe(lTelephoneFixe.getValue());
				etudiant.setTelephoneportable(lTelephonePortable.getValue());
				etudiant.setAdressenumero((new Integer(lAdresseNumero
						.getValue())).intValue());
				etudiant.setAdressevoie(lAdresseVoie.getValue());
				etudiant.setAdressecodepostal(lAdresseCodePostal.getValue());
				etudiant.setAdresseville(lAdresseVille.getValue());
				etudiant.setAdressepays(lAdressePays.getValue());

				final InfoMessageBox lInfoMessageBox = new InfoMessageBox(
						"Mise à jour en cours...");
				lInfoMessageBox.open();
				AsyncCallback lCallback = new AsyncCallback() {

					public void onFailure(Throwable pCaught) {
						lInfoMessageBox.close();
						FailureMessageBox lFailureMessageBox = new FailureMessageBox(
								"Erreur mise à jour", pCaught.getMessage());
						lFailureMessageBox.open();
					}

					public void onSuccess(Object pResult) {
						lInfoMessageBox.close();
						Info.show("Information", "Mis à jour avec succès", "");
						profilContent.remove(profilEditingPanel);
						readingMode();
					}
				};

				controlerService.updateEtudiant(etudiant, lCallback);
			}
		});
		lFormPanel.add(lSubmit);

		profilContent.add(profilEditingPanel);
	}

	private void readingMode() {
		if (etudiant == null) {
			FailureMessageBox lFailureMessageBox = new FailureMessageBox(
					"Erreur accès étudiant",
					"L'étudiant n'est pas bien authentifié");
			lFailureMessageBox.show();
		}
		else {
			profilReadingPanel = etudiant.getHTMLProfil();
			profilContent.add(profilReadingPanel);	
		}
		
	}

	public VerticalPanel getPanel() {
		return panel;
	}

	public void setPanel(VerticalPanel panel) {
		this.panel = panel;
	}

	public ContentPanel getProfilPanel() {
		return profilPanel;
	}

	public void setProfilPanel(ContentPanel profilPanel) {
		this.profilPanel = profilPanel;
	}

	public VerticalPanel getProfilContent() {
		return profilContent;
	}

	public void setProfilContent(VerticalPanel profilContent) {
		this.profilContent = profilContent;
	}

	public HTML getProfilReadingPanel() {
		return profilReadingPanel;
	}

	public void setProfilReadingPanel(HTML profilReadingPanel) {
		this.profilReadingPanel = profilReadingPanel;
	}

	public FormPanel getProfilEditingPanel() {
		return profilEditingPanel;
	}

	public void setProfilEditingPanel(FormPanel profilEditingPanel) {
		this.profilEditingPanel = profilEditingPanel;
	}

	public EtudiantVOGWT getEtudiant() {
		return etudiant;
	}

	public void setEtudiant(EtudiantVOGWT etudiant) {
		this.etudiant = etudiant;
	}

	public boolean isEditionMode() {
		return editionMode;
	}

	public void setEditionMode(boolean editionMode) {
		this.editionMode = editionMode;
	}

	public ControlerServiceAsync getControlerService() {
		return controlerService;
	}

	public void setControlerService(ControlerServiceAsync controlerService) {
		this.controlerService = controlerService;
	}
}
